package com.ruoyi.project.pm.pview.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.pm.pview.domain.PmPview;
import com.ruoyi.project.pm.pview.service.PmPviewService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/pm/pview")
public class PmPviewController extends BaseController {
        //声明业务层属性
        @Autowired
        private PmPviewService pmPviewServiceImpl;
        //声明资源跳转的公共前缀
        private String prefix = "pm/pview";
        //声明单元方法:完成项目类型管理页面跳转
        @RequiresPermissions("pm:pview:view")
        @GetMapping()
        public String cmsType(){
            return prefix+"/wnm";
        }
        //声明单元方法：完成项目类型的加载
        @RequiresPermissions("pm:pview:list")
        @PostMapping("/list")
        @ResponseBody
        public TableDataInfo list(PmPview pmPview)
        {
            startPage();
            List<PmPview> list = pmPviewServiceImpl.selectCmsTypeList(pmPview);
            return getDataTable(list);
        }

        //查看：
        @RequiresPermissions("pm:pview:add")
        @GetMapping("/add")
        public String add()
        {
            return prefix + "/add";
        }

        /**
         * 删除公告类别
         * @param ids
         * @return
         */
        @RequiresPermissions("pm:pview:remove")
        @Log(title = "项目类别管理", businessType = BusinessType.DELETE)
        @PostMapping("/remove")
        @ResponseBody
        public AjaxResult remove(String ids)
        {
            try
            {
                return toAjax(pmPviewServiceImpl.deleteTypeByIds(ids));
            }
            catch (Exception e)
            {
                return error(e.getMessage());
            }
        }
        /**
         * 修改项目，跳转修改页面并完成数据的回显
         */
        @GetMapping("/edit/{proId}")
        public String edit(@PathVariable("proId") Integer proId , ModelMap mmap)
        {
            mmap.put("pview", pmPviewServiceImpl.selectTypeById(proId));
            return prefix + "/edit";
        }
        /**
         * 修改保存项目类别
         */
        @RequiresPermissions("pm:pview:edit")
        @Log(title = "项目管理", businessType = BusinessType.UPDATE)
        @PostMapping("/edit")
        @ResponseBody
        public AjaxResult editSave(@Validated PmPview pmPview)
        {
            if (UserConstants.POST_NAME_NOT_UNIQUE.equals(pmPviewServiceImpl.checkTypeNameUnique(pmPview)))
            {
                return error("修改项目类别'" + pmPview.getProType() + "'失败，类别名称已存在");
            }
            return toAjax(pmPviewServiceImpl.updateCmsType(pmPview));
        }
        /**
         * 校验类型名称
         */
        @PostMapping("/checkTypeNameUnique")
        @ResponseBody
        public String checkPostNameUnique(PmPview pmPview)
        {
            return pmPviewServiceImpl.checkTypeNameUnique(pmPview);
        }
    }



